package cn.stylefeng.guns.modular.rest;


import cn.hutool.core.date.DateUtil;
import cn.stylefeng.guns.modular.ht.entity.Report;
import cn.stylefeng.guns.modular.ht.service.ReportService;
import cn.stylefeng.guns.modular.school.entity.ClassInfo;
import cn.stylefeng.guns.modular.school.entity.ClassMsg;
import cn.stylefeng.guns.modular.school.entity.StudentGuardian;
import cn.stylefeng.guns.modular.school.entity.StudentInfo;
import cn.stylefeng.guns.modular.school.service.ClassInfoService;
import cn.stylefeng.guns.modular.school.service.ClassMsgService;
import cn.stylefeng.guns.modular.school.service.StudentGuardianService;
import cn.stylefeng.guns.modular.school.service.StudentInfoService;
import cn.stylefeng.guns.sys.core.constant.factory.ConstantFactory;
import cn.stylefeng.guns.sys.core.properties.GunsProperties;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.guns.sys.modular.system.entity.FileInfo;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.FileInfoService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ErrorResponseData;
import cn.stylefeng.roses.core.reqres.response.SuccessResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


/**
 * 绑定控制器
 *
 * @author
 * @Date 2019-11-18 13:24:11
 */
@RestController
@RequestMapping("/gunsApi/binding")
public class ApiNeedTokenBindingController extends BaseController {
    @Autowired
    private UserService userService;
    @Autowired
    private ReportService reportService;

    /**
     * 绑定
     */
    @RequestMapping(value = "")
    public Object binding(String username, String pwd, String avatarUrl) {
        Map map = new HashMap();
        SuccessResponseData SUCCESS_TIP = new SuccessResponseData();

        User user = userService.getByAccount(username);
        String password = ShiroKit.md5(pwd, user.getSalt());

        if (user != null && StringUtils.equals(user.getPassword(), password)) {
            // 更新微信头像
            if (!StringUtils.equals("undefined", avatarUrl)) {
                user.setWxAvatar(avatarUrl);
                userService.updateById(user);
            }
            SUCCESS_TIP.setData(map);

            map.put("user", user);
            map.put("now", DateUtil.now());

            //String today = DateUtil.today();
            //String tomorrow = DateUtil.format(DateUtil.tomorrow(), "yyyy-MM-dd");
            // 查看今天是否已经上报
            //Report report = reportService.getOne(new QueryWrapper<Report>().eq("account", username).gt("report_time", today).lt("report_time", tomorrow));
            //map.put("report", report);

            return SUCCESS_TIP;
        } else {
            return new ErrorResponseData(500, "用户或者密码不正确！");
        }

    }

}


